﻿using SMALLERP.DataClass;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace XJLSC.PO
{
    public partial class POPRICE : Form
    {
        private readonly DataBase db = new DataBase();
        private SqlDataReader sdr1;
        public POPRICE()
        {
            InitializeComponent();
        }

        private void POPRICE_Load(object sender, EventArgs e)
        {
            tabPage2.Parent = null;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            String strSql = "";
            if(radioButton1.Checked==true)
            {
                strSql = "SELECT A.ITEM_PROPERTY 类型,ISNULL(G.SUPPLY_CENTER_NAME,'无品号采购信息') 采购域,ISNULL(D.SUPPLIER_CODE,'') 供应商编号,ISNULL(D.SUPPLIER_NAME,'') 供应商,B.ITEM_CODE 品号,B.ITEM_NAME 品名,B.ITEM_SPECIFICATION 规格,B.ITEM_DESC 描述,B.DRAWING_NO 图号,B.CreateDate 创建日期 FROM ITEM_PLANT A LEFT JOIN ITEM_PURCHASE F ON A.ITEM_ID=F.ITEM_ID LEFT JOIN ITEM B ON A.ITEM_ID=B.ITEM_BUSINESS_ID LEFT JOIN ITEM_SUPPLIER C ON C.ITEM_BUSINESS_ID=F.ITEM_BUSINESS_ID LEFT JOIN SUPPLIER D ON D.SUPPLIER_BUSINESS_ID=C.SUPPLIER_ID LEFT JOIN SUPPLY_CENTER G ON G.SUPPLY_CENTER_ID=F.Owner_Org_ROid WHERE A.ApproveStatus='Y' AND (A.ITEM_PROPERTY='P' OR A.ITEM_PROPERTY='S') AND NOT EXISTS(SELECT * FROM ITEM_SUPPLIER_PRICE C WHERE A.ITEM_ID=C.ITEM_ID) ORDER BY A.ITEM_PROPERTY,B.ITEM_CODE";
            }
            else
            {
                strSql = "SELECT (CASE WHEN PRICE_TYPE='1' THEN 'P' ELSE 'S' END) 类型,ISNULL(G.SUPPLY_CENTER_NAME,'无品号采购信息') 采购域,ISNULL(D.SUPPLIER_CODE,'') 供应商编号,ISNULL(D.SUPPLIER_NAME,'') 供应商,B.ITEM_CODE 品号,B.ITEM_NAME 品名,B.ITEM_SPECIFICATION 规格,B.ITEM_DESC 描述,B.DRAWING_NO 图号,INEFFECTIVE_DATE 失效日期 FROM ITEM_SUPPLIER_PRICE A LEFT JOIN ITEM B ON A.ITEM_ID=B.ITEM_BUSINESS_ID LEFT JOIN SUPPLIER D ON D.SUPPLIER_BUSINESS_ID=A.SUPPLIER_ID LEFT JOIN SUPPLY_CENTER G ON G.SUPPLY_CENTER_ID=A.Owner_Org_ROid ";
                strSql += " WHERE A.ApproveStatus = 'Y' AND DATEDIFF(DAY, GETDATE(), INEFFECTIVE_DATE)<= 10 AND NOT EXISTS(SELECT * FROM ITEM_SUPPLIER_PRICE C WHERE A.ITEM_ID = C.ITEM_ID AND A.INEFFECTIVE_DATE < C.INEFFECTIVE_DATE)";
            }
            //textBox1.Text = strSql;
            try
            {
                DataTable dt;
                this.gridControl1.DataSource = "";
                this.gridControl1.Refresh();
                dt = db.GetDataSet1(strSql, "MOCTA").Tables["MOCTA"];
                this.gridControl1.DataSource = dt;
            }
            catch
            {

            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            SaveFileDialog fileDialog = new SaveFileDialog();
            fileDialog.Title = "导出Excel";
            fileDialog.Filter = "Excel文件(*.xls)|*.xls";
            DialogResult dialogResult = fileDialog.ShowDialog(this);
            if (dialogResult == DialogResult.OK)
            {
                DevExpress.XtraPrinting.XlsExportOptions options = new DevExpress.XtraPrinting.XlsExportOptions();
                gridControl1.ExportToXls(fileDialog.FileName);
                DevExpress.XtraEditors.XtraMessageBox.Show("保存成功！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }
    }
}
